import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter2/colors.dart';
import 'package:flutter2/http/api.dart';
import 'package:flutter2/http/dio_manager.dart';
import 'package:flutter2/model/order_entity.dart';
import 'package:flutter2/model/sheet_item_entity.dart';
import 'package:flutter2/ui/sheet.dart';
import 'package:flutter2/utils/num.dart';
import 'package:flutter2/utils/string.dart';
import 'package:flutter2/widget/base/base_container.dart';
import 'package:flutter2/widget/base/normal_button.dart';
import 'package:flutter2/widget/base/normal_text.dart';
import 'package:flutter2/widget/info_bar.dart';
import 'package:flutter2/widget/info_edit.dart';
import 'package:flutter2/widget/title_bar.dart';
import 'package:flutter2/widget/top_tip.dart';
import 'package:flutter_pickers/pickers.dart';
import 'package:flutter_pickers/style/picker_style.dart';
import 'package:flutter_pickers/time_picker/model/pduration.dart';
import 'package:get/get.dart';

class AddCardPage extends StatefulWidget {
  const AddCardPage({
    super.key,
    required this.loan,
    this.orderEntity,
  });

  final bool loan;
  final OrderEntity? orderEntity;

  @override
  State<AddCardPage> createState() => _AddCardPageState();
}

class _AddCardPageState extends State<AddCardPage> {
  SheetItemEntity? cardType;
  SheetItemEntity? bank;

  String birth = "";
  TextEditingController noCon = TextEditingController();
  TextEditingController emailCon = TextEditingController();

  @override
  void initState() {
    super.initState();
    noCon.addListener(() => setState(() {}));
    emailCon.addListener(() => setState(() {}));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: mainBg,
      appBar: TitleBar.buildAppBar(title: "银行卡"),
      body: SafeArea(
        child: Column(
          children: [
            Flexible(
              child: SingleChildScrollView(
                physics: BouncingScrollPhysics(),
                child: Column(
                  children: [
                    TopTip(title: "银行卡"),
                    InfoBar(
                      value: cardType?.value,
                      title: "卡片类型",
                      click: () {
                        SheetData.getSheetData(
                            type: "waistsymphonypercentage",
                            call: (item) {
                              cardType = item;
                              setState(() {});
                            });
                      },
                    ),
                    InfoBar(
                      value: bank?.value,
                      title: "银行",
                      click: () {
                        SheetData.getSheetData(
                            type: "minibusaware",
                            call: (item) {
                              bank = item;
                              setState(() {});
                            });
                      },
                    ),
                    InfoEdit(
                      title: "银行卡号",
                      controller: noCon,
                      inputType: TextInputType.number,
                      inputFormatters: [FilteringTextInputFormatter.digitsOnly],
                    ),
                    if (widget.loan) ...getMore(context),
                  ],
                ),
              ),
            ),
            NormalButton(
              enable: enable(),
              width: double.infinity,
              margin: EdgeInsets.symmetric(horizontal: 16, vertical: 20),
              text: "提交",
              click: () {
                if (widget.loan && !emailCon.text.isEmail) {
                  return;
                }
                var param = getParams();
                param["shutcard"] = bank?.code;
                param["protectionsuckmemory"] = bank?.value;
                param["waistsymphonypercentage"] = cardType?.code;
                param["candidatestraightforward"] = noCon.text;
                if (widget.loan) {
                  param["workertour"] = birth;
                  param["scientistworriedengland"] = emailCon.text;
                }
                if (widget.orderEntity != null) {
                  param["unbearablecreate"] = "1";
                  param["correspondacquiregrass"] = widget.orderEntity?.orderId;
                }
                DioManager.request(
                  path: HttpApi.addBankCard,
                  data: param,
                  onSuccess: (data) {},
                );
              },
            )
          ],
        ),
      ),
    );
  }

  bool enable() {
    return cardType?.isNotNull() == true &&
        bank?.isNotNull() == true &&
        !noCon.text.isNull() &&
        (!widget.loan || birth.isNotEmpty && !emailCon.text.isNull());
  }

  List<Widget> getMore(BuildContext context) {
    return [
      InfoBar(
        value: birth,
        title: "生日",
        click: () {
          Pickers.showDatePicker(
            context,
            minDate: PDuration(year: 1950, month: 1, day: 1),
            maxDate: PDuration(year: 2012, month: 12, day: 31),
            selectDate: PDuration(year: 1998, month: 1, day: 1),
            pickerStyle: PickerStyle(
              textSize: 15,
              cancelButton: BaseContainer(
                padding: EdgeInsets.symmetric(horizontal: 15),
                child: NormalText(
                  text: "Cancel",
                  fontSize: 16,
                  fontColor: mainText3,
                ),
              ),
              commitButton: BaseContainer(
                padding: EdgeInsets.symmetric(horizontal: 15),
                child: NormalText(
                  text: "Confirm",
                  fontColor: btnColor,
                  fontSize: 16,
                ),
              ),
            ),
            onConfirm: (res) {
              int day = res.day ?? 1;
              int month = res.month ?? 1;
              birth = "${day.appendZero()}-${month.appendZero()}-${res.year}";
              setState(() {});
            },
          );
        },
      ),
      InfoEdit(
        title: "邮箱",
        controller: emailCon,
      ),
    ];
  }
}
